<?php

class Search {
	var $size="";
	var $length="";
	var $base="";
}

class DbFactory {

	public function __construct($host="localhost", $dbname="catalogo" , $user="user" , $pass="pass"){
		try {
			$this->db = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass ,
					array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch (PDOException $e) {
			error_log("PDOException: " . $e->getMessage());
			die();
		}
	}

	public function find($attributo,$filter=""){
		try {
			$ar = array();
			if($this->db!=null){
				$sql = "SELECT DISTINCT ".$attributo." FROM prodotti ";
				if($filter!='')
					$sql .= "  WHERE ".$filter." ORDER BY ".$attributo;
				else 
					$sql .= " ORDER BY ".$attributo;
				error_log("SQL:".$sql);
				$stmt = $this->db->prepare($sql);
				$stmt->execute();
				while ($row = $stmt->fetch()) {
					array_push($ar,$row[0]);
				}
			}
		}catch (Exception $e) {
			error_log("Exception!: " . $e->getMessage());
			die();
		}
		return $ar;
	}
	
	public function product($attributo,$filter=""){
		try {
			$ar = array();
			if($this->db!=null){
				$sql = "SELECT DISTINCT ".$attributo." FROM prodotti ";
				if($filter!='')
					$sql .= "  WHERE ".$filter." ORDER BY ".$attributo;
				else
					$sql .= " ORDER BY ".$attributo;
				error_log("SQL:".$sql);
				$stmt = $this->db->prepare($sql);
				$stmt->execute();
				while ($row = $stmt->fetch()) {
					array_push($ar,$row);
				}
			}
		}catch (Exception $e) {
			error_log("Exception!: " . $e->getMessage());
			die();
		}
		return $ar;
	}
	
}

